package com.caoji.item.web;

import com.caoji.dto.PageDTO;
import com.caoji.entity.SkuDTO;
import com.caoji.entity.SpecParamDTO;
import com.caoji.entity.SpuDTO;
import com.caoji.entity.SpuDetailDTO;
import com.caoji.item.service.SkuService;
import com.caoji.item.service.SpuDetailService;
import com.caoji.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService detailService;
    @Autowired
    private SkuService skuService;


    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querypage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id){
            return ResponseEntity.ok(spuService.querypage(page,rows,saleable,categoryId,brandId,id));
    }
    @PostMapping("/spu")
    public ResponseEntity<Void> addspu(@RequestBody SpuDTO spuDTO){
            spuService.addspu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> findparam(@RequestParam(value = "id",required = true)Long id,
                                                        @RequestParam(value = "searching",required = false) boolean searching){
      return  ResponseEntity.ok(spuService.findparam(id,searching));
    }
//根据id批量查询sku
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> qyerysku(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(skuService.qyerysku(ids));
    }

    //根据id查询商品详情
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> qyerydetail(@RequestParam("id")Long id){
        return ResponseEntity.ok(detailService.qyerydetail(id));
    }

    //根据id查询spu及sku、spuDetail等
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> qyeryspualls(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.qyeryspualls(id));
    }

    //根据id查询商品
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> qyeryspu(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.qyeryspu(id));
    }

    //根据spuID查询sku的集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> qyeryskuall(@RequestParam("id")Long id){
        return ResponseEntity.ok(skuService.qyeryskuall(id));
    }

    @PutMapping("/saleable")
    public ResponseEntity<Void> isSaleable(@RequestParam("id") Long id,@RequestParam("saleable")Boolean saleable){
        spuService.isSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("spu")
    public ResponseEntity<Void> updategoods(@RequestBody SpuDTO spuDTO){
        spuService.updategoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



}